<template>
  <div class="page">
    <!--  头部  -->
    <div class="head">
      <el-image style="width: 62px; height: 36px" :src="logo" fit="fit"></el-image>
      <span class="titletext ml20">{{title}}</span>
    </div>
    <!--  中间  -->
    <div class="middle">
      <div class="login-form">
        <h4 class="login-title  ">用户登录</h4>
        <el-divider></el-divider>

        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username">
            <el-input ref="username" v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号"
              @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
              @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
              @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code" @click="getCode">
              <span class="login-code-img">{{ codeUrl }}</span>
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住账号</el-checkbox>
          <el-form-item style="width:100%;">
            <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right;"><router-link class="link-type" to="/forgot">忘记密码</router-link></div>
          </el-form-item>
        </el-form>
      </div>

    </div>
    <!--  底部  -->
    <div class="footer">
      <span>{{copyright}}</span>
    </div>

   <vue-particles class="vue-particles" color="#FFFFFF"
   :particleOpacity="0.7" :particlesNumber="80" shapeType="polygon"
         :particleSize="4" linesColor="#FFFFFF" :linesWidth="1"
         :lineLinked="true" :lineOpacity="0.4" :linesDistance="150"
         :moveSpeed="2" :hoverEffect="false" hoverMode="grab"
         :clickEffect="false"  clickMode="push" ref="particles">
   </vue-particles>

  </div>

</template>

<script>
  import {
    getCodeImg,
    getTitle,
    getRight
  } from '@/api/login';
  import Cookies from 'js-cookie';
  import {
    encrypt,
    decrypt
  } from '@/utils/jsencrypt';
  import logoImg from '@/assets/logo/logo.png';
  export default {
    name: 'Login',
    data() {
      return {
        title: "盐都区疾病预防控制中心电子档案管理平台", //process.env.VUE_APP_TITLE,
        copyright: '',
        codeUrl: '',
        loginForm: {
          username: '',
          password: '',
          rememberMe: false,
          code: '',
          uuid: ''
        },
        loginRules: {
          username: [{
            required: true,
            trigger: 'blur',
            message: '请输入您的账号'
          }],
          password: [{
            required: true,
            trigger: 'blur',
            message: '请输入您的密码'
          }],
          code: [{
            required: true,
            trigger: 'blur',
            message: '请输入验证码'
          }]
        },
        loading: false,
        // 验证码开关
        captchaEnabled: true,
        // 注册开关
        register: false,
        redirect: undefined,
        logo: logoImg
      };
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      this.get_itle();
      this.getCookie();
      this.getCode();
      this.$nextTick(() => {
        this.$refs.username.$refs.input.focus();
      });

    },
    methods: {
      onkeyup(e) {


      },
      get_itle() {
        var Params = {

        }
        getTitle(Params).then(response => {
          this.title = response.data ? response.data : this.title;
        });

        getRight(Params).then(response => {
          this.copyright = response.data ? response.data : this.copyright;
        });

      },
      getCode() {
        const Parmas = {
          uuid: this.loginForm.uuid
        }
        getCodeImg(Parmas).then(res => {
          this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
          if (this.captchaEnabled) {
            this.codeUrl = res.captcha_code;
            this.loginForm.uuid = res.uuid;
          }
        });
      },
      getCookie() {
        const username = Cookies.get('username');
        const rememberMe = Cookies.get('rememberMe');
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
        };
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set('username', this.loginForm.username, {
                expires: 30
              });
              Cookies.set('rememberMe', this.loginForm.rememberMe, {
                expires: 30
              });
            } else {
              Cookies.remove('username');
              Cookies.remove('rememberMe');
            }

            this.$store
              .dispatch('Login', this.loginForm)
              .then(() => {
                this.$router.push({
                  path: this.redirect || '/'
                }).catch(() => {});
              })
              .catch(() => {
                this.loading = false;
                this.loginForm.code = '';
                if (this.captchaEnabled) {
                  this.getCode();
                }
              });
          }
        });
      }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss">
  .page {
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
  }

  .head {
    height: 120px;
    display: flex;
    align-items: center;
    padding-left: 80px;
  }

  .titletext {
    font-weight: bold;
    font-size: 32px;
    color: #3D7DE0;
    letter-spacing: 2px;
  }

  .middle {
    flex: 1;
    background: url(../assets/images/m_bg.png) no-repeat center;
    background-color: #3D7DE0;
    display: flex;
    align-items: center;

  }

  .footer {
    height: 120px;
    line-height: 96px;
    width: 100%;
    text-align: center;
    font-family: Arial;
    font-size: 14px;
    letter-spacing: 1px;
  }

  .login-form {
    border-radius: 6px;
    position: absolute;
    right: 10%;
    // margin-top: 20px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
z-index: 99999;
    .login-title {
      color: #3D7DE0;
      font-weight: bold;
      font-size: 24px;
      text-align: center;
   letter-spacing: 5px;
      margin-bottom: 20px;
    }

    .el-input {
      height: 38px;

      input {
        height: 38px;
        // padding-bottom: 10px;
        text-indent: 5px;
        background: transparent;
        border: none;
        border-radius: 0;
        color: #333;
        border-bottom: 1px solid rgb(235, 237, 242);
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }

    .el-button--small {
      padding: 16px;
      font-size: 16px;
    }
  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  // .login-code {
  //   width: 33%;
  //   height: 38px;
  //   float: right;
  //   img {
  //     cursor: pointer;
  //     vertical-align: middle;
  //   }
  // }
  .login-code {
    display: flex;
    align-items: center;
    justify-content: space-around;
    margin: 0 0 0 10px;
    width: 33%;
    height: 38px;
    float: right;
    cursor: pointer;

    .login-code-img {
      margin-top: 2px;
      width: 100px;
      height: 38px;
      background-color: #fdfdfd;
      border: 1px solid #f0f0f0;
      color: #333;
      font-size: 18px;
      font-weight: bold;
      letter-spacing: 5px;
      line-height: 38px;
      text-indent: 5px;
      text-align: center;
    }
  }
  #particles-js {
    position: absolute; /* 为防止页面有其他的内容时会挤下去 */
    // height: 100%;
    // width: 100%;
    overflow: hidden;
    top: 120px;
    left: 10px;
    right: 10px;
    bottom: 100px;;
  }


</style>
